package com.onenine.app.fragment;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Pair;
import android.view.View;
import android.widget.ImageView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.onenine.app.R;
import com.onenine.app.activity.MainActivity;
import com.onenine.app.activity.PostDetailActivity;
import com.onenine.app.adapter.DynamicAdapter;
import com.onenine.app.adapter.MultiImageAdapter;
import com.onenine.app.event.ImageTransitionEvent;
import com.onenine.app.model.DynamicItem;
import com.onenine.app.request.PageRequest;
import com.onenine.app.response.DynamicListResponse;
import com.onenine.app.utils.DataType;
import com.onenine.app.utils.UrlConfig;
import com.onenine.app.view.TransitionViewHelper;
import com.onenine.app.view.nineimage.NineGridImageView;
import com.onenine.base.base.BasePageListFragment;
import com.onenine.base.okhttp.OkCallback;
import com.onenine.base.okhttp.OkRequest;
import com.onenine.base.utils.AppConfig;
import com.onenine.base.view.PullRefreshRecyclerView;
import com.onenine.chat.rongcloud.RcUser;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

/**
 * 关注对象的动态
 */
public class CAttentionFragment extends BasePageListFragment implements MultiImageAdapter.OnItemImageClickListener {

    private NineGridImageView mClickMultiImageView;

    @Override
    protected void initView(View view) {
        super.initView(view);
    }

    @Override
    protected int getLayout() {
        return R.layout.layout_pull_refresh_view;
    }

    @Override
    protected Pair<PullRefreshRecyclerView, RecyclerView.Adapter> initPullRefreshView(View view) {
        return new Pair(view.findViewById(R.id.pull_to_refresh_layout), new DynamicAdapter(false, this));
    }

    @Override
    public void initData() {
        super.initData();
        mPullRefreshView.postDelayed(new Runnable() {
            @Override
            public void run() {
                loadPageData(1);
            }
        }, 500);
    }

    @Override
    protected void loadPageData(int pageNum) {
        PageRequest request = new PageRequest();
        request.setUserId(RcUser.getInstance().getUserId());
        request.setPageSize(AppConfig.PAGE_SIZE);
        request.setPageNum(pageNum);
        OkRequest.getInstance().withUrl(UrlConfig.GET_ATTENTION_DYNAMIC)
                .withRequest(request).doRequest(CAttentionFragment.class.getSimpleName(), new OkCallback<DynamicListResponse>() {
            @Override
            public void onResponse(DynamicListResponse response, String srcData) {
                dismissRefreshStatus();
                if (response.isSuccess()) {
                    notifyDataSuccess(response.getDynamicItemList());
                } else {
                    notifyDataFailure();
                }
            }
        });
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        //动态详情
        DynamicItem dynamicItem = (DynamicItem) adapter.getItem(position);
        PostDetailActivity.launchActivity(getActivity()
                , dynamicItem.getDynamickPkId(), DataType.POST_DYNAMIC, dynamicItem.getSpanType());
    }

    @Override
    public void onItemClick(Context context, NineGridImageView multiImageView, ImageView imageView) {
        mClickMultiImageView = multiImageView;
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(ImageTransitionEvent event) {
        if (TextUtils.equals(event.eventName
                , MainActivity.class.getSimpleName()) && event.position >= 0) {
            //多图查看返回
            if (mClickMultiImageView != null) {
                TransitionViewHelper.onActivityReenter(getActivity()
                        , mClickMultiImageView.getImageView(event.position));
            }
        }
    }

}
